<?php

/**
 * 批量用表格添加数据
 */

namespace app\fuck\model;

use think\Db;
use think\Model;

class XzszModel extends Model
{
    protected $pk = 'id';
    protected $name = '';
    protected $type = ['more' => 'array',];
    protected $autoWriteTimestamp = true;   //自动写入时间戳

    /**
     * 高一，将老师表里的老师写入数课表基础数据表
     */
    public function save_ls($d, $njmc, $xnxq, $zc, $xq)
    {
        $num = 0;
        Db::startTrans();
        try {
            foreach ($d as $k => $v) {
                $w['xnxq'] = $xnxq;
                $w['njmc'] = $njmc;
                $w['lsxm'] = $v;
                $id = Db::name('kb_base_ls')->where($w)->value('id');
                if (empty($id)) {
                    $data = ['type' => 1, 'xxmc' => '新洲三中', 'lsxm' => $v, 'xnxq' => $xnxq, 'njmc' => $njmc, 'create_time' => time()];
                    $id = Db::name('kb_base_ls')->insert($data, true); //直接替换->insertGetId($data);
                    if ($id > 0) $num++;
                } else {
                    $data = ['type' => 1, 'xxmc' => '新洲三中', 'lsxm' => $v, 'xnxq' => $xnxq, 'njmc' => $njmc, 'update_time' => time()];
                    $id = Db::name('kb_base_ls')->where('id', $id)->update($data); //直接替换->insertGetId($data);
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            var_dump($e);
            return false;
        }

        return $num;
    }

    /**
     * 高一，将老师表里的任课老师写入数课表基础数据表
     */
    public function save_kc_ls($d, $njmc, $xnxq, $zc, $xq)
    {
        //var_dump("老师表数据：");
        //var_dump($d);
        $num = 0;
        Db::startTrans();
        try {
            foreach ($d as $k1 => $v1) {    //遍历行
                if (empty($v1)) continue;
                $bjmc = $v1['bj']; //班级名称
                $bzr = $v1['bzr']; //班主任
                $yw = $v1['yw']; //语文
                $sx = $v1['sx']; //数学
                $yy = $v1['yy'];
                $wl = $v1['wl'];
                $hx = $v1['hx'];
                $sx = $v1['sx'];
                $sw = $v1['sw'];
                $zz = $v1['zz'];
                $lishi = $v1['lishi'];
                $dili = $v1['dili'];
                $tiyu = $v1['tiyu'];
                $yinyue = $v1['yinyue'];
                $meishu = $v1['meishu'];
                $xinxi = $v1['xinxi'];
                $tonyon = $v1['tonyon'];
                $jswz = $v1['jswz'];        //教室位置
                foreach ($v1 as $k2 => $v2) {
                    if ($k2 == "bj" || $k2 == "bzr" || $k2 == "jswz") continue;

                    $data = ['xxmc' => '新洲三中', 'xnxq' => $xnxq, 'njmc' => $njmc, 'bjmc' => $bjmc, 'kcmc' => $k2, 'lsxm' => $v2, 'create_time' => time()];
                    $id = Db::name('kb_kc_ls')->where("xnxq='$xnxq' and njmc='$njmc' and bjmc='$bjmc' and kcmc='${k2}' ")->value('id');
                    if (empty($id)) {
                        $id = Db::name('kb_kc_ls')->insert($data, true); //直接替换->insertGetId($data);
                        if ($id > 0) $num++;
                    } else {
                        $id = Db::name('kb_kc_ls')->where('id', $id)->update($data);
                    }
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            var_dump($e);
            return false;
        }

        return $num;
    }

    /**
     * 高一，转换并写入课表，单行分析
     */
    public function save_kb_bj($d, $njmc, $xnxq)
    {
        $ret = $this->create_List($d, $njmc, $xnxq);
        if (empty($ret)) {
            return false;
        }

        Db::startTrans();
        try {
            foreach ($ret as $k => $v) {
                //取得各字段id号
                $w = [];
                $w['xnxq'] = $xnxq;
                $w['njmc'] = $njmc;
                $w['lsxm'] = $v['lsxm'];
                $ls_bh = Db::name('kb_base_ls')->where($w)->value('id');
                if (!empty($ls_bh)) {
                    $v['ls_id'] = $ls_bh;
                }

                $w = [];
                $w['xnxq'] = $xnxq;
                $w['njmc'] = $njmc;
                $w['bjjc'] = $v['bjmc'];
                $w['delete_time'] = 0;
                $bj = Db::name('kb_base_bj')->where($w)->find();
                if (empty($bj) || $bj == null) {
                    //如果没有此班级，则不要此行数据
                    continue;
                }
                var_dump("bj:");
                var_dump($bj);
                $v['bjmc'] = $bj['bjmc'];
                $v['bj_id'] = $bj['bj_id'];
                $v['csmc'] = $bj['csmc'];
                $v['csbm'] = $bj['csbm'];

                $v['create_time'] = time();
                $v['xxmc'] = '新洲三中';

                $w1 = [];
                $w1['xnxq'] = $xnxq;
                $w1['njmc'] = $njmc;
                $w1['bjmc'] = $v['bj_mc'];
                //$w1['zc'] = $v['zc'];
                $w1['xq'] = $v['xq'];
                $w1['jc'] = $v['jc'];
                //$w1['kcmc'] = $v['kcmc'];
                //$id = Db::name('kb_bj_kb')->where($w1)->value('id');
                Db::name('kb_bj_kb')->where($w1)->delete();
                $id = "";
                $fields = "xxmc,xnxq,zc,xq,jc,njmc,bjmc,kcmc,lsxm,bj_id,kc_id,ls_id,csmc,csbm,create_time";
                if (empty($id)) {
                    Db::name('kb_bj_kb')->field($fields)->insertGetId($v, true);
                } else {
                    $id = Db::name('kb_bj_kb')->field($fields)->where('id', $id)->update($v);
                }
            }
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ["code" => 0, "data" => $ret, "msg" => $e->getMessage()];
        }

        return ["code" => 1, "data" => $ret, "msg" => "提交成功"];;
    }

    /**
     * 生成课程表数据，返回arr
     */
    public function create_List($d, $njmc, $xnxq)
    {
        if (empty($d)) return false;    //第一行是空行
        if (empty($d['kc_mc'])) return false; //课程名称为空则不处理

        $ret = [];

        var_dump("一行课表上青天：");
        var_dump($d);

        $d['bjmc'] = $d['bj_mc'];
        $d['njmc'] = $njmc;

        $v_zc = $this->zf_zc($d['zc']);
        unset($d['zc']);

        //求kc_mc的老师
        //'语文', '数学', '英语', '物理', '化学', '生物', '政治', '历史', '地理', '体育', '音乐', '美术', '信息', '通用技术'
        $w['xnxq'] = $xnxq;
        $w['njmc'] = $njmc;
        $w['bjmc'] = $d['bj_mc'];

        foreach ($v_zc as $k => $v) {       //遍历单双周
            if ($d['kc_mc'] == "班会") {
                $w['kcmc'] = 'zixi';
                $d['kcmc'] = "班会";
                $d['lsxm'] = "无";
                $d['ls_id'] = "9999";
            } else if ($d['kc_mc'] == "语") {
                $w['kcmc'] = 'yw';
                $d['kcmc'] = "语文";
            } else if ($d['kc_mc'] == "数") {
                $w['kcmc'] = 'sx';
                $d['kcmc'] = "数学";
            } else if ($d['kc_mc'] == "英") {
                $w['kcmc'] = 'yy';
                $d['kcmc'] = "英语";
            } else if ($d['kc_mc'] == "物") {
                $w['kcmc'] = 'wl';
                $d['kcmc'] = "物理";
            } else if ($d['kc_mc'] == "化") {
                $w['kcmc'] = 'hx';
                $d['kcmc'] = "化学";
            } else if ($d['kc_mc'] == "生") {
                $w['kcmc'] = 'sw';
                $d['kcmc'] = "生物";
            } else if ($d['kc_mc'] == "政") {
                $w['kcmc'] = 'zz';
                $d['kcmc'] = "政治";
            } else if ($d['kc_mc'] == "历") {
                $w['kcmc'] = 'lishi';
                $d['kcmc'] = "历史";
            } else if ($d['kc_mc'] == "地") {
                $w['kcmc'] = 'dili';
                $d['kcmc'] = "地理";
            } else if ($d['kc_mc'] == "体") {
                $w['kcmc'] = 'tiyu';
                $d['kcmc'] = "体育";
            } else if ($d['kc_mc'] == "音") {
                $w['kcmc'] = 'yinyue';
                $d['kcmc'] = "音乐";
            } else if ($d['kc_mc'] == "美") {
                $w['kcmc'] = 'meishu';
                $d['kcmc'] = "美术";
            } else if ($d['kc_mc'] == "信息") {
                $w['kcmc'] = 'xinxi';
                $d['kcmc'] = "信息";
            } else if ($d['kc_mc'] == "通用技术") {
                $w['kcmc'] = 'tonyon';
                $d['kcmc'] = "通用技术";
            } else if ($d['kc_mc'] == "自习-语") {
                $w['kcmc'] = 'yw';
                $d['kcmc'] = "语文自习";
            } else if ($d['kc_mc'] == "自习-数") {
                $w['kcmc'] = 'sx';
                $d['kcmc'] = "数学自习";
            } else if ($d['kc_mc'] == "自习-英") {
                $w['kcmc'] = 'yy';
                $d['kcmc'] = "英语自习";
            } else if ($d['kc_mc'] == "自习-生") {
                $w['kcmc'] = 'sw';
                $d['kcmc'] = "生物自习";
            } else if ($d['kc_mc'] == "自习-物") {
                $w['kcmc'] = 'wl';
                $d['kcmc'] = "物理自习";
            } else if ($d['kc_mc'] == "自习-化") {
                $w['kcmc'] = 'hx';
                $d['kcmc'] = "化学自习";
            } else if ($d['kc_mc'] == "信/技") {   ////信息技术分单双周
                var_dump("分析分单双周的课程：信/技");
                var_dump("周次：", $k);
                if ($k == 'dz') {
                    $w['kcmc'] = 'xinxi';
                    $d['kcmc'] = "信息";
                } else if ($k == 'sz') {
                    $w['kcmc'] = 'tonyon';
                    $d['kcmc'] = "通用技术";
                }
            } else if ($d['kc_mc'] == "美/音") {   ////音乐分单双周
                var_dump("分析分单双周的课程：美/音");
                var_dump("周次：", $k);
                if ($k == 'dz') {
                    $w['kcmc'] = 'yinyue';
                    $d['kcmc'] = "音乐";
                } else if ($k == 'sz') {
                    $w['kcmc'] = 'meishu';
                    $d['kcmc'] = "美术";
                }
            } else if ($d['kc_mc'] == "语/英") {   ////音乐分单双周
                var_dump("分析分单双周的课程：语/英");
                var_dump("周次：", $k);
                if ($k == 'dz') {
                    $w['kcmc'] = 'yw';
                    $d['kcmc'] = "语文";
                } else if ($k == 'sz') {
                    $w['kcmc'] = 'yy';
                    $d['kcmc'] = "英语";
                }
            } else if (strpos($d['kc_mc'], "自习-语/英")) {   //周三晚自习分单双周
                var_dump("分析分单双周的课程：自习-语/英");
                var_dump("周次：", $k);
                if ($k == 'dz') {
                    $w['kcmc'] = 'yw';
                    $d['kcmc'] = "语文自习";
                } else if ($k == 'sz') {
                    $w['kcmc'] = 'yy';
                    $d['kcmc'] = "英语自习";
                }
            } else if ($d['kc_mc'] = '自习') {
                $w['kcmc'] = 'zixi';
                $d['kcmc'] = "自习";
                $d['lsxm'] = "无";
                $d['ls_id'] = "9999";
            }

            $d['zc'] = implode(",", $v);

            $v_lsxm = Db::name('kb_kc_ls')->where($w)->value('lsxm');
            if (!empty($v_lsxm)) {
                $d['lsxm'] = $v_lsxm;
            }

            $w1 = [];
            $w1['xnxq'] = $xnxq;
            $w1['njmc'] = $njmc;
            $w1['kcmc'] = $w['kcmc'];
            $kc_bh = Db::name('kb_base_kc')->where($w1)->value('id');
            $d['kc_id'] = $kc_bh;

            $ret[] = $d;
        }

        var_dump("转换后数据：");
        var_dump($ret);
        return $ret;
    }

    /**
     * 分析单行课表文字 ，转换成二维表
     * 输入："bjjc"=>"一（1）","kcjc"=>"语","xnxq"=>"202302","zc"=>"1,2,3-23","jc"=>"1","xq"=>"1","njmc" => "1"
     * 输出："bjmc","kcmc","bj_id","kc_id","lsxm","ls_id","xnxq","zc","jc","xq","njmc"
     */
    public function zf_kb_xzszg1nj($d)
    {

        if (!strpos($d['kc_mc'], '/') && !strpos($d['kc_mc'], '-')) {
        }

        if (strpos($d['kc_mc'], '-')) {
            $_t = explode("-", $d['kc_mc']);
            if ($_t == '自习') {
            }
        }

        if (strpos($d['kc_mc'], '/')) {
        }
    }

    /**
     * 分析1-23,24,26这样的以逗号分割的周次
     */
    public function zf_zc($zc)
    {
        $v_zc = [];
        $ar = explode(",", $zc);
        foreach ($ar as $k => $v) {
            $ok = strpos($v, "-");
            if ($ok) {
                $a = explode("-", $v);
                $c1 = intval($a[0]);
                $c2 = intval($a[1]);
                for ($i = $c1; $i < $c2; $i++) {
                    array_push($v_zc, $i);
                }
            } else {
                if (is_numeric($v)) array_push($v_zc, intval($v));
            }
        }

        //分单双周
        $zc_dz = [];
        $zc_sz = [];
        foreach ($v_zc as $k => $v) {
            if ($v % 2 == 1) {
                //奇数
                $zc_dz[] = $v;
            } else if ($v % 2 == 0) {
                //偶数
                $zc_sz[] = $v;
            }
        }

        return [
            //"zc" => $v_zc,
            "dz" => $zc_dz,
            "sz" => $zc_sz,
        ];
    }
}
